using UnityEngine;

public class Singleton<T> where T : Singleton<T>, new()
{
    private static T _instance;
    public static T Instance => GetInstance();
    public static T I => GetInstance();
    public static T GetInstance()
    {
        if (_instance == null)
        {
            _instance = new T();
        }
        return _instance;
    }
}

public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
    private static T _instance;
    public static T Instance => GetInstance();
    public static T I => GetInstance();
    public static T GetInstance()
    {
        if (_instance == null)
        {
            _instance = new GameObject(typeof(T).Name).AddComponent<T>();
        }
        return _instance;
    }

    protected virtual void Awake()
    {
        if (_instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            _instance = this as T;
        }
    }
}
